package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.zzyl.dto.BedDto;
import com.zzyl.entity.Bed;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.BedMapper;
import com.zzyl.service.BedService;
import com.zzyl.utils.ObjectUtil;
import com.zzyl.vo.BedVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Service
public class BedServiceImpl implements BedService {

    @Autowired
    private BedMapper bedMapper;

    @Override
    public List<BedVo> getBedsByRoomId(Long roomId) {
        return bedMapper.getBedsByRoomId(roomId);
    }

    /**
     * 新增床位
     * @param bedDto
     */
    @Override
    public void addBed(BedDto bedDto) {
        //参数校验
        String bedNumber = bedDto.getBedNumber();
        Integer sort = bedDto.getSort();
        Long roomId = bedDto.getRoomId();
        if (ObjectUtil.isEmpty(bedNumber)||ObjectUtil.isEmpty(sort)||ObjectUtil.isEmpty(roomId)) {
            throw new BaseException(BasicEnum.BED_INSERT_FAIL);
        }
        //业务
        Bed bed = new Bed();
        bed.setCreateTime(LocalDateTime.now());
        bed.setUpdateBy(1L);
        bed.setUpdateBy(1L);
        bedDto.setBedStatus(0);
        BeanUtils.copyProperties(bedDto, bed);
        Bed beds = bedMapper.selectByNumber(bedNumber);
        if (Objects.nonNull(beds)) {
            throw new BaseException(BasicEnum.BED_INSERT_FAIL);
        }
        bedMapper.addBed(bed);
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @Override
    public BedVo getById(Long id) {
        Bed bed = bedMapper.getById(id);
        return BeanUtil.toBean(bed,BedVo.class);
    }

    /**
     * 修改床位
     * @param bedDto
     */
    @Override
    public void updateBed(BedDto bedDto) {
        //参数校验
        String bedNumber = bedDto.getBedNumber();
        Integer sort = bedDto.getSort();
        if (ObjectUtil.isEmpty(bedNumber)||ObjectUtil.isEmpty(sort)) {
            throw new BaseException(BasicEnum.BED_UPDATE_FAIL);
        }
        //业务
        Bed bed1 = new Bed();
        bed1.setUpdateTime(LocalDateTime.now());
        bed1.setUpdateBy(1L);
        BeanUtils.copyProperties(bedDto,bed1);
//        Bed bed =bedMapper.selectByNumber(bedNumber);
//        if (Objects.nonNull(bed)) {
//            throw new BaseException(BasicEnum.BED_UPDATE_FAIL);
//        }
        bedMapper.updateBed(bed1);
    }

    /**
     * 删除床位
     * @param id
     */
    @Override
    public int delBed(Long id) {
        //根据床位id查询创维信息
        Bed bed =bedMapper.selectById(id);
        //如果床位不存在则删除失败
        if (Objects.isNull(bed)) {
            return 0;
        }
        //如果状态为0 可以删除,其他不能删除
        if(bed.getBedStatus() !=0){
            return 0;
        }
        int del = bedMapper.deleteById(id);
        return del;
    }
}

